//
//  CircleAnimationView.swift
//  SwiftUIDemo
//
//  Created by zhouzuosong on 2020/8/24.
//  Copyright © 2020 Kael. All rights reserved.
//

import SwiftUI

struct CircleAnimationView: View {
    
    @State var isLongPress = false
    
    var body: some View {
        VStack {
            BannerText(pageTitle: "Trigger", text: "圆形触发器动画", backgroundColor: .green)
            
            Text(self.isLongPress ? "已按下" : "还原")
            
            Spacer()
            
            Circle()
                .fill(self.isLongPress ? Color.blue : Color.green)
                .frame(width: 200, height: 200, alignment: .center)
                .overlay(Text("long press"))
                .overlay(
                    Circle()
                        .stroke(Color.black, lineWidth: 6)
                        .scaleEffect(self.isLongPress ? 2 : 1)
                        .opacity(self.isLongPress ? 0 : 1)
                )
                .onLongPressGesture {
                    self.isLongPress.toggle()
                }
                .animation(.default)
            
            Spacer()

            Circle()
                .fill(self.isLongPress ? Color.blue : Color.green)
                .frame(width: 200, height: 200, alignment: .center)
                .overlay(Text("long press"))
                .padding()
                .overlay(
                    Circle()
                        .trim(from: 0, to: self.isLongPress ? 0 : 1)
                        .stroke(self.isLongPress ? Color.blue : Color.green, style: StrokeStyle(lineWidth: 20, lineCap: CGLineCap.round))
                        .rotationEffect(.degrees(-90))
                        .animation(Animation.linear(duration: 1))
                    
                )
                .onLongPressGesture {
                    self.isLongPress.toggle()
                }
                .animation(.default)
            
            Spacer()
        }
    }
}

struct CircleAnimationView_Previews: PreviewProvider {
    static var previews: some View {
        CircleAnimationView()
    }
}
